package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Set;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        //查询待接单数量
        Integer waitingOrderCount = orderMapper.countStatistics(Orders.TO_BE_CONFIRMED);
        waitingOrderCount = waitingOrderCount == null ? 0 : waitingOrderCount;
        //待派送数量
        Integer deliveredOrderCount = orderMapper.countStatistics(Orders.DELIVERY_IN_PROGRESS);
        deliveredOrderCount = deliveredOrderCount == null ? 0 : deliveredOrderCount;
        //已完成数量
        Integer completedOrderCount = orderMapper.countStatistics(Orders.COMPLETED);
        completedOrderCount = completedOrderCount == null ? 0 : completedOrderCount;
        //已取消数量
        Integer cancelledOrderCount = orderMapper.countStatistics(Orders.CANCELLED);
        cancelledOrderCount = cancelledOrderCount == null ? 0 : cancelledOrderCount;
        //全部订单
        Integer allOrders = orderMapper.countAll();
        allOrders = allOrders == null ? 0 : allOrders;

        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .completedOrders(completedOrderCount)
                .deliveredOrders(deliveredOrderCount)
                .cancelledOrders(cancelledOrderCount)
                .waitingOrders(waitingOrderCount)
                .build();

        return orderOverViewVO;
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        //查询已起售数量
        Integer start = dishMapper.findByStatus(StatusConstant.ENABLE);

        //查询已停售数量
        Integer shop = dishMapper.findByStatus(StatusConstant.DISABLE);

        DishOverViewVO overViewVO = DishOverViewVO.builder()
                .sold(start)
                .discontinued(shop)
                .build();

        return overViewVO;
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        //已起售数量
        Integer sold = setmealMapper.findByStatus(StatusConstant.ENABLE);
        Integer discountinued = setmealMapper.findByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discountinued)
                .build();
    }

    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData() {
        //查询总营业额
        HashMap<String, Object> map = new HashMap<>();
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0 : turnover;
        //查询有效订单
        map.put("status",Orders.COMPLETED);
        Integer validOrderCount = orderMapper.countStatistics(Orders.COMPLETED);
        validOrderCount = validOrderCount == null ? 0 : validOrderCount;
        Integer orderCompleted = validOrderCount;

        //查询平均客单价
        Integer total = orderMapper.countAll();
        total = total == null ? 0 : total;
        Double unitPrice = total == 0 ? 0 : turnover / total;
        unitPrice = Math.round(unitPrice * 100.0) / 100.0;

        //订单完成率
        Double orderCompletionRate = Double.valueOf(total == 0 ? 0 : validOrderCount / total);

        //查询用户
        Integer newUser = userMapper.countUser();
        newUser = newUser == null ? 0 : newUser;

        return BusinessDataVO.builder()
                .newUsers(newUser)
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();

    }
}
